package classifier;

import java.util.List;

import learner.Pattern;
import learner.hmm.AlgorithmValueCalculator;
import learner.hmm.HMMModel;
import learner.hmm.Movement;

public class HMMClassifier {

	private List<HMMModel> models;

	public HMMClassifier(List<HMMModel> patternModels) {
		models = patternModels;
	}

	public String classify(Pattern givenPattern) {
		try {
			Movement movement = new Movement(givenPattern);
			Movement thresholdMovement = movement.permute();
			double maxChance = 0.0;
			HMMModel maxModel = null;
			for (HMMModel model : models) {
				AlgorithmValueCalculator calc = new AlgorithmValueCalculator(
						model, movement);
				AlgorithmValueCalculator thresholdCalc = new AlgorithmValueCalculator(
						model, thresholdMovement);
				if (maxChance < calc.getChanceOnPattern() && calc.getChanceOnPattern() > thresholdCalc.getChanceOnPattern()) {
					maxChance = calc.getChanceOnPattern();
					maxModel = model;
				}
			}
			return "HMM detected " + maxModel.getName() + " with probability :"
					+ maxChance;
		} catch (IllegalArgumentException e) {
			return "Movement to short";
		} catch (NullPointerException e) {
			return "No model found";
		}
	}

}
